package com.cxd.web.backend;

import com.cxd.entity.EbNews;
import com.cxd.entity.EbUser;
import com.cxd.service.IEbNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller //标记为服务员
@RequestMapping( "/news" ) //工号
public class NewsController {
    @Autowired //spring 注入 给newsService赋值
    private IEbNewsService newsService;

    /**@RequestParam("page") page必须与请求参数的名字一直
     * spring mvc自动将请求参数的值绑定list方法的psize参数
     * ?page=2 ==spring mvc==》 psize=2
     * @RequestParam(value = "page" , defaultValue = "1")
     * defaultValue=1表示如果请求没有携带page参数，则psize的默认值为1
     */
    @RequestMapping("/list" )
    public String list(HttpSession session, Model model, @RequestParam(value="page",defaultValue = "1") int psize,
                       @RequestParam(value = "pageSize",defaultValue ="3") int pageSize){//alt+enter) {//alt+enter
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        System.out.println("start service.....;psize==>"+psize);
        //1.调用Service层的类的方法获取分页的新闻数据
        List<EbNews> newsList = newsService.listByPage(psize,3);
        //2.调用Service层的类的方法获取总页数
        final Integer totalPage = newsService.findTotalPage(pageSize);
        //3.将model和jsp整合   kely-value键值对
        //3.1 model中绑定分页的新闻数据
        model.addAttribute(  "newsList" , newsList);
        //3.2 mode l中绑定总页数
        model.addAttribute( "totalPage" , totalPage);
        //3.3 model中绑定当前页
        model.addAttribute (  "currPage" ,psize);
        //3.4 jsp
        model.addAttribute("tag",6);
        return "manage/news";//manage/news.jsp


    }
    @RequestMapping("/to_save")
    public String toSave(HttpSession session,Model model){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        // spingmvc框架:前缀/WEB—INF/ manage/news-add.jsp后缀
        model.addAttribute("tag",6);
        return "manage/news-add";
    }
    @RequestMapping("/save")
    public String save(HttpSession session,EbNews ebNews,Model model) {
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        System.out.println("ebNews==>"+ebNews);
        //l1.调用service层的添加新闻的方法
        //result 返回时受影响的行数
        Integer result = newsService.save(ebNews);
        System.out.println("result:"+result);
        //2.根据result 的结果>0?决定跳转网页
        model.addAttribute("tag",6);
        model.addAttribute("result",result);
        return "manage/manage-result";//manage/manage-result.jsp
    }
    @RequestMapping("/to_edit")
    public String toEdit(HttpSession session,Model model,
                         @RequestParam("enId") int enId){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        System.out.println("点击修改跳过来了,enId="+enId);
        //1.调用service层的方法查询点击的新闻的所有信息
        EbNews ebNews = newsService.selectOne(enId);
        //2.指定model和jsp
        model.addAttribute("ebNews",ebNews);
        model.addAttribute("tag",6);
        return "manage/news-modify";//manage/news-modify.jsp
    }
    @RequestMapping("/edit")
    public String edit(HttpSession session,EbNews ebNews,Model model){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        System.out.println("ebNews==>"+ebNews);
        //1.调用service层的更新新闻的方法
        Integer result = newsService.updateNews(ebNews);
        //2.根据更新返回过来的结果决定跳转页面
        model.addAttribute("tag",6);
        model.addAttribute("result",result);
        //更新成功的情况
        return "manage/manage-result";
    }
    @RequestMapping("/delete")
    public String delete(HttpSession session,Integer id,Model model){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        //1.参数校验

        //2.执行删除（真实项目中 delete from 表名 where id=?）
        int result =newsService.deleteByPk(id);
        model.addAttribute("tag",6);
        model.addAttribute("result",result);
        return "manage/manage-result";
    }

}

